package rolesystem.middle.utilityclasses;

import java.util.ArrayList;

import rolesystem.agentlogic.utilityclasses.AgentLog;
import rolesystem.middle.managers.DataOutputManager;
import rolesystem.middle.parameters.DefaultParametersFile;


public class AggregateData 
{
	/**
	 * Data of the agents "living" in the simulation
	 */
	private ArrayList<AgentLog> agentData;
	
	/**
	 * Timestamp of the start time of the simulation
	 */
	private long startTime;
	
	/**
	 * Timestamp of the end time of the simulation
	 */
	private long endTime;
	
	public AggregateData()
	{
		agentData= new ArrayList<AgentLog>();
	}
	
	public synchronized void setEndTime(long time)
	{
		this.endTime=time;
	}
	
	public synchronized void setStartTime(long time)
	{
		this.startTime=time;
	}
	
	public synchronized long getStartTime()
	{
		return this.startTime;
	}
	
	public synchronized void createAgentData(int numberOfAgents)
	{
		for(int agent_id=0;agent_id<numberOfAgents;agent_id++)
			this.agentData.add(agent_id, new AgentLog());
	}
	
	public synchronized void clearAgentData()
	{	
		this.agentData.clear();	
	}
	
	public synchronized void setAgentData(int agent_id,AgentLog data)
	{
		try 
		{
			this.agentData.set(agent_id, data);
		} catch (IndexOutOfBoundsException  e) 
		{
			DataOutputManager.printString("ERROR: trying to set statistical data for an invalid id");
		}
	}
	
	public synchronized AgentLog getAgentData(int agent_id)
	{
		return this.agentData.get(agent_id);
	}
	
	public synchronized AgentLog getAgentDataNull(int agent_id)
	{
		try 
		{
			return this.agentData.get(agent_id);
		} catch (IndexOutOfBoundsException  e) 
		{
			DataOutputManager.printString("ERROR: statistic requested with invalid id");
			return null;
		}
	}
	
	public synchronized String toString()
	{
		String output;
		
		output="Time" + DefaultParametersFile.data_separator + String.valueOf(startTime)+ DefaultParametersFile.data_separator +String.valueOf(endTime) + DefaultParametersFile.row_separator+ DefaultParametersFile.line_separator;
		output+= "Agents" + DefaultParametersFile.data_separator + String.valueOf(agentData.size()) + DefaultParametersFile.row_separator+ DefaultParametersFile.line_separator;

		for(int i=0;i<agentData.size();i++)
		{
			output+= "AgentN" + DefaultParametersFile.data_separator + String.valueOf(i) + DefaultParametersFile.data_separator 
					+ agentData.get(i).getAllAsString()
				    + DefaultParametersFile.row_separator + DefaultParametersFile.line_separator;
		}	
		
		output+= DefaultParametersFile.line_separator;
		
		return output;
	}
	
	public synchronized String toStringPeriodic()
	{
		String output="";

		for(int i=0;i<agentData.size();i++)
		{
			output+= "AgentN" + DefaultParametersFile.data_separator + String.valueOf(i) + DefaultParametersFile.data_separator
					+ agentData.get(i).getAllAsString()
				    + DefaultParametersFile.row_separator + DefaultParametersFile.line_separator;
		}	
		
		return output;
	}
	
}
